package org.example.ui;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;

public class RunButtonListener implements KeyListener {

    private String confPath;

    private JTextArea jtOutput;

    public RunButtonListener(String confPath, JTextArea jtOutput) {
        this.confPath = confPath;
        this.jtOutput = jtOutput;
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // 按下调用
        checkConfFile();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // 按下调用
        checkConfFile();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // 释放调用
    }

    private void checkConfFile() {
        StringBuilder output = new StringBuilder();
        output.append(jtOutput.getText()).append(System.lineSeparator());
        File file;
        try {
            file = new File(confPath);
            output.append("配置文件加载完成!");
            jtOutput.setText(output.toString());
        } catch (Exception e) {
            output.append("配置文件加载异常！").append(e).append(System.lineSeparator());
            jtOutput.setText(output.toString());
        }
    }
}
